<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        
    </style>
</head>
<body>
    <input type="button" id="btn1" value="开启计时器">
    <input type="button" id="btn2" value="停止计时器">

    <input type="button" id="btn3" value="开启延时器">
    <input type="button" id="btn4" value="停止延时器">
</body>
<script>
    var btn1 = document.getElementById("btn1");
    var btn2 = document.getElementById("btn2");

    var t;
    
    btn1.onclick = function(){
        clearInterval( t );
        t = setInterval(function(){
            console.log("hello");
        }, 1000);
    }
    
    btn2.onclick = function(){
        clearInterval( t );
    }

    // 注意：当前程序中不要保留没有意义的计时器
    // 如果是在行为内开启计时器，在开启之前尽量先关闭

    var btn3 = document.getElementById("btn3");
    var btn4 = document.getElementById("btn4");

    var t2;
    
    btn3.onclick = function(){
        t2 = setTimeout(function(){
            console.log("world");
        }, 3000);
    }
    btn4.onclick = function(){
        clearTimeout(t2);
    }


</script>
</html>